package com.example.park_management.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.park_management.common.PageParam;
import com.example.park_management.common.Result;
import com.example.park_management.domain.Departments;
import com.example.park_management.domain.Notices;
import com.example.park_management.service.DepartmentsService;
import com.example.park_management.service.EmployeesService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


@RestController
@RequestMapping("/departments")
public class DepartmentsController {

    @Resource
    DepartmentsService departmentsService;
    @Resource
    EmployeesService employeesService;
    @PostMapping("/")
    public Result add(@RequestBody Departments departments) {
        try{
            if (StringUtils.isBlank(departments.getDepartmentName())) {
                return Result.fail("必填属性为空");
            }
        /*if (departments.getEmployeesId() != null) { // 确保employeeId不为null才进行检查
            boolean employeeExists = employeesService.existsById(departments.getEmployeesId());
            if (!employeeExists) {
                return Result.fail("指定的员工ID不存在");
            }
        }*/
            boolean save = departmentsService.save(departments);
            return save ? Result.success() : Result.fail();
        }catch (Exception e) {
            return Result.fail("输入格式异常！");
        }

    }

    @PutMapping("/")
    public Result update(@RequestBody Departments departments) {
        try{
            if (StringUtils.isBlank(departments.getDepartmentName())) {
                return Result.fail("必填属性为空");
            }
            boolean b = departmentsService.updateById(departments);
            return b ? Result.success() : Result.fail();
        }catch (Exception e) {
            // ownerId格式不正确，记录错误或处理异常
            return Result.fail("所有者ID格式错误");
        }
    }

    @DeleteMapping("/{departmentId}")
    public Result delete(@PathVariable Integer departmentId) {
        boolean b = departmentsService.removeById(departmentId);
        return b ? Result.success() : Result.fail();
    }

    @PostMapping("/page")
    public Result page(@RequestBody PageParam<Departments> pageParam) {
        Page<Departments> departmentsPage = new Page<>(pageParam.getPageNum(), pageParam.getPageSize());
//        departmentsService.page(departmentsPage);

        QueryWrapper<Departments> departmentsQueryWrapper = new QueryWrapper<>();
        if (pageParam.getData() != null){

            if (pageParam.getData().getDepartmentName() != null){
                departmentsQueryWrapper.like("department_name", pageParam.getData().getDepartmentName());
            }
        }
        departmentsService.page(departmentsPage,departmentsQueryWrapper);
        return Result.success(departmentsPage.getRecords(),departmentsPage.getTotal());
    }

}
